# node_types = set([
#     "BusbarSection",      # 母线段
#     "LoadBreakSwitch",    # 负荷开关
#     "Substation",         # 变电站
#     "TransformerWinding", # 变压器绕组
#     "ACLineSegment",      # 线路
#     "Breaker",            # 开关
#     "BUSBAR_VIRTUAL",     # 母线
#     "Fuse",               # 保险丝
#     "Disconnector",       # 隔离开关 
#     "GroundDisconnector", # 接地刀闸
#     ]
# )
import numpy as np
class Node():
    def __init__(
            self,
            base_voltage,
            capacity,
            is_open,
            rdf_id,
            node_type,
            is_virtual_node,
            from_list,
            to_list,
            ) -> None:
        self.data = {
            "base_voltage": base_voltage,
            "rated_capacity": capacity,
            "capacity": capacity,
            "is_open": is_open,
            "rdf_id": rdf_id,
            "node_type": node_type,
            "is_virtual_node": is_virtual_node,
            "from_list":from_list,
            "to_list": to_list,
            "load":[],
            "users":{},
            "best_path":[],
        }
    def  __getitem__(self, key):
        assert key in self.data
        return self.data[key]
    
    def __setitem__(self, key, value):
        assert key in self.data
        self.data[key] = value
    
    def is_open(self):
        return self.data["is_open"]

    def get_id(self):
        return self.data["rdf_id"]
    
    def get_history_loads(self):
        return self.data["load"]
    
    def get_capacity(self):
        if self.data["capacity"] is None:
            return np.inf
        return self.data["capacity"]
    
    def get_load_rate(self, load_value):
        if self.data["capacity"] is None:
            return 0
        else:
            return (self.data["rated_capacity"] - self.data["capacity"] + load_value)/(self.data["rated_capacity"]+1e-7)
        
    
    def get_load_list(self):
        return self.data["load"]
    
    def get_from_list(self):
        return self.data["from_list"]
    
    def get_to_list(self):
        return self.data["to_list"]
    
    def add_load(self, id, load_value, hard = False):
        self.data["users"][id] = load_value
        if not self.data["capacity"] is None:
            if not hard:
                assert self.data["capacity"] >= load_value
            self.data["capacity"] -= load_value
            
    
    def release_load(self, id):
        assert id in self.data["users"]
        if self.data["capacity"] is None:
            return
        load_value = self.data["users"].pop(id)
        self.data["capacity"] +=load_value
    
    def update_best_path(self,best_path, best_is_keys):
        self.data["best_path"] = [best_path,best_is_keys]

